<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="活动开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择活动开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择活动结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:subRule:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column
        label="活动开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="活动结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属商城" align="center" prop="mallName" />
      <el-table-column label="补贴金额" align="center" prop="subPrice" />
      <el-table-column label="补贴总数" align="center" prop="subTotalNum" />

      <el-table-column
        label="用户周期可领取的个数"
        align="center"
        prop="userReceiveNum"
      />
      <el-table-column
        label="用户单天可领取个数"
        align="center"
        prop="userReceiveDayNum"
      />
      <el-table-column label="时间周期" align="center" prop="ruleType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.ruleType == 1"
            >有效期内任意时间</el-tag
          >
          <el-tag type="danger" v-if="scope.row.ruleType == 2"
            >有效期内规则时间-月</el-tag
          >
          <el-tag type="warning" v-if="scope.row.ruleType == 3"
            >有效期内规则时间-周</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="补贴配置方" align="center" prop="configSource">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.configSource == 1">省行配置</el-tag>
          <el-tag type="warning" v-if="scope.row.configSource == 2"
            >内部收银台-月</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="isOnSale">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isOnSale"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:subRule:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:subRule:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改补贴规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择活动开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择活动结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="补贴金额" prop="subPrice">
          <el-input
            v-model="form.subPrice"
            placeholder="请输入补贴金额"
            onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
          />
        </el-form-item>

        <el-form-item label="时间周期" prop="ruleType">
          <el-radio-group v-model="form.ruleType">
            <el-radio :label="1">有效期内任意时间</el-radio>
            <el-radio :label="2">有效期内规则时间-月</el-radio>
            <el-radio :label="3">有效期内规则时间-周</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否启用" prop="isOnSale">
          <el-radio-group v-model="form.isOnSale">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户周期可领取个数" prop="userReceiveNum">
          <el-input-number
            v-model="form.userReceiveNum"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="用户单天可领取个数 " prop="userReceiveDayNum">
          <el-input-number
            v-model="form.userReceiveDayNum"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="补贴总数" prop="subTotalNum">
          <el-input-number
            v-model="form.subTotalNum"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="补贴配置方" prop="configSource">
          <el-radio-group v-model="form.configSource">
            <el-radio :label="1">省行配置</el-radio>
            <el-radio :label="2">内部收银台</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRule,
  getRule,
  delRule,
  addRule,
  updateRule,
  changeOnSale,
} from "@/api/mallSystem/rule";

export default {
  name: "Rule",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补贴规则表格数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询补贴规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then((response) => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: null,
        endTime: null,
        subTotalNum: null,
        subPrice: null,
        ruleType: 1,
        isOnSale: 1,
        userReceiveNum: null,
        userReceiveDayNum: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加补贴规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRule(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改补贴规则";
      });
    },
    // 是否启用
    handleStatusChange(row) {
      console.log(row);
      const { id, isOnSale } = row;
      let text = row.isOnSale == 1 ? "启用" : "关闭";
      this.$modal
        .confirm("确认要" + text + "吗？")
        .then(function () {
          return changeOnSale({ id, isOnSale });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isShelf = row.isShelf == 1 ? 0 : 1;
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除此数据项？")
        .then(function () {
          return delRule(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/rule/export",
        {
          ...this.queryParams,
        },
        `rule_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
